package com.google.code.openperfmon.web.common;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionProxy;

public class MenuHelperBean {
	private String currentNamespace;
	private String currentAction;
	
	public static final String CSS_ACTIVE_TAB = "ui-state-active";
	public static final String CSS_SELECTED_TAB = " ui-tabs-selected " + CSS_ACTIVE_TAB;
	public static final String CSS_INACTIVE_MAIN = "ui-state-default ui-corner-top";
	public static final String CSS_ACTIVE_MAIN = CSS_INACTIVE_MAIN + CSS_SELECTED_TAB;
	public static final String CSS_UL_MAIN = "ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all";
	public static final String CSS_INACTIVE_SUB = ""; 
	public static final String CSS_ACTIVE_SUB = "active";
	
	public MenuHelperBean() {
		HttpServletRequest rq = ServletActionContext.getRequest();
		ActionContext ctx = ServletActionContext.getActionContext(rq); 
		ActionProxy proxy = ctx.getActionInvocation().getProxy(); 
		
		currentNamespace = proxy.getNamespace();
		currentAction = proxy.getActionName();
	}
	
	public String mainClass(String ns) {
		return currentNamespace.equals(ns) ? CSS_ACTIVE_MAIN : CSS_INACTIVE_MAIN; 
	}
	
	public String getUlClass() {
		return CSS_UL_MAIN;
	}
	
	public String getActiveTab() {
		return CSS_ACTIVE_TAB;
	}
	
	public String getSelectedTab() {
		return CSS_SELECTED_TAB;
	}
	
	public String showSubs(String ns) {
		return currentNamespace.equals(ns) ? "display:block" : "display:none";
	}
	
	public String subClass(String ns, String act) {
		return currentNamespace.equals(ns) && 
			(currentAction.equals(act) || sameActionGroup(act))
			? CSS_ACTIVE_SUB : CSS_INACTIVE_SUB;
	}
	
	private boolean sameActionGroup(String act) {
		String actGroup = act.replace("list", "");
		String currentGroup = currentAction.replace("addEdit", "") + "s";
		return currentGroup.equals(actGroup);
	}
}
